package org.lendoo.web.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.pojo.common.ApiResult;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.dto.HotGoodsDto;
import org.lendoo.web.pojo.dto.RelGoodsImageUrlDto;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.lendoo.web.entity.Goods;
import org.lendoo.web.pojo.vo.GoodsVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Goods的controller类
 */
@Api(value = "Goods的controller类", tags = "GoodsController")
@RestController
@RequestMapping("api/v1/goods")
@Slf4j
public class GoodsController extends AbstractController {

//    /**
//     * 新增
//     * @param goodsDto Goods的dto类
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("新增")
//    @ApiImplicitParam(name = "goodsDto", value = "Goods的dto类", required = true, dataType = "GoodsDto")
//    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> add(@RequestBody GoodsDto goodsDto, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == goodsDto) {
//            String message = "调用接口【/api/v1/goods/add时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        Goods goods = new Goods();
//        BeanUtils.copyProperties(goodsDto, goods);
//        ServiceResult<Boolean> serviceResult = goodsService.add(goods);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 批量导入
//     * @param goodsDtoList Goods的dto类列表
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("批量导入")
//    @ApiImplicitParam(name = "goodsDtoList", value = "Goods的dto类列表", required = true, dataType = "List")
//    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> batchAdd(@RequestBody List<GoodsDto> goodsDtoList, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == goodsDtoList) {
//            String message = "调用接口【/api/v1/goods/batchAdd时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        List<Goods> goodsList = new ArrayList<>();
//        BeanUtils.copyProperties(goodsDtoList, goodsList);
//        ServiceResult<Boolean> serviceResult = goodsService.batchAdd(goodsList);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 根据id更新
//     * @param goodsDto Goods的dto类
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("根据id更新")
//    @ApiImplicitParam(name = "goodsDto", value = "Goods的dto类", required = true, dataType = "GoodsDto")
//    @RequestMapping(value = "/updateForId", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> updateForId(@RequestBody GoodsDto goodsDto, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == goodsDto) {
//            String message = "调用接口【/api/v1/goods/updateForId时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        Goods goods = new Goods();
//        BeanUtils.copyProperties(goodsDto, goods);
//        ServiceResult<Boolean> serviceResult = goodsService.updateForId(goods);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }
//
//    /**
//     * 根据id删除
//     * @param id 主键
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("根据id删除")
//    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
//    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<Boolean>> deleteById(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//        if (null == id) {
//            String message = "调用接口【/api/v1/goods/deleteById时，参数不合法";
//            log.warn(message);
//            apiResult.setSuccess(Boolean.FALSE);
//            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
//            apiResult.setMessage(message);
//            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
//        }
//
//        ServiceResult<Boolean> serviceResult = goodsService.deleteById(id);
//
//        apiResult.setSuccess(serviceResult.getSuccess());
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }

    /**
     * 根据商品id，查询对应的图片列表
     * @param goodsId 主键
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("根据商品id，查询对应的图片列表")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
    @RequestMapping(value = "/findGoodsAndImageUrlListByGoodsId/{goodsId}", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<RelGoodsImageUrlDto>> findGoodsAndImageUrlListByGoodsId(@PathVariable("goodsId") Integer goodsId, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == goodsId) {
            String message = "调用接口【/api/v1/goods/findGoodsAndImageUrlListByGoodsId时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        ServiceResult<RelGoodsImageUrlDto> serviceResult = goodsService.findGoodsAndImageUrlListByGoodsId(goodsId);

        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }


    /**
     * 根据条件，分页查询，升序/降序排列
     * @param goodsVo Goods的dto类
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("根据条件，分页查询，升序/降序排列")
    @ApiImplicitParam(name = "goodsDto", value = "Goods的dto类", required = true, dataType = "GoodsDto")
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<PageInfo<List<RelGoodsImageUrlDto>>>> page(@RequestBody GoodsVo goodsVo, HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();
        if (null == goodsVo) {
            String message = "调用接口【/api/v1/goods/page时，参数不合法";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }
        if (null == goodsVo.getPageNo() || null == goodsVo.getPageSize()) {
            String message = "调用接口【/api/v1/goods/page时，参数pageNo和pageSize不能为空";
            log.warn(message);
            apiResult.setSuccess(Boolean.FALSE);
            apiResult.setCode(HttpStatus.BAD_REQUEST.value());
            apiResult.setMessage(message);
            return new ResponseEntity<>(apiResult, HttpStatus.BAD_REQUEST);
        }

        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo, goods);
        ServiceResult<PageInfo<List<RelGoodsImageUrlDto>>> serviceResult = goodsService.page(goods, goodsVo.getPageNo(), goodsVo.getPageSize());

        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }

//    /**
//     * 查询所有记录
//     * @param request
//     * @param response
//     * @return
//     */
//    @ApiOperation("查询所有记录")
//    @ApiImplicitParam(name = "", value = "", required = true, dataType = "")
//    @RequestMapping(value = "/findAll", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
//    @ResponseBody
//    public ResponseEntity<ApiResult<List<Goods>>> findAll(HttpServletRequest request, HttpServletResponse response) {
//        ApiResult apiResult = new ApiResult();
//
//        ServiceResult<List<Goods>> serviceResult = goodsService.findAll();
//
//        apiResult.setSuccess(Boolean.TRUE);
//        apiResult.setCode(HttpStatus.OK.value());
//        apiResult.setMessage(serviceResult.getMessage());
//        apiResult.setResult(serviceResult.getResult());
//        return new ResponseEntity<>(apiResult, HttpStatus.OK);
//    }

    /**
     * 查找所有热门商品
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("查找所有热门商品")
    @ApiImplicitParam(name = "", value = "", required = true, dataType = "")
    @RequestMapping(value = "/findHotGoods", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ResponseBody
    public ResponseEntity<ApiResult<List<HotGoodsDto>>> findHotGoods(HttpServletRequest request, HttpServletResponse response) {
        ApiResult apiResult = new ApiResult();

        ServiceResult<List<HotGoodsDto>> serviceResult = goodsService.findHotGoods();

        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setMessage(serviceResult.getMessage());
        apiResult.setResult(serviceResult.getResult());
        return new ResponseEntity<>(apiResult, HttpStatus.OK);
    }
}
